#include "ParticleEmitter.h"
namespace FireCloud{
ParticleEmitter::ParticleEmitter(ParticleEmitterData ped){
	m_particle_emitter_data = ped;
	m_new_frame_number = 0;
	srand((unsigned int)time(NULL));
}
void ParticleEmitter::draw(){
	ParticleNode *particle_node;
	GLvector speed;
	GLcolor start_color;
	GLcolor end_color;
	GLfloat start_size;
	GLfloat end_size;
	GLfloat angle;
	GLfloat disperse;
	GLuint life;
	if(m_list_node.size()<m_particle_emitter_data.particle_max_sum){
		m_new_frame_number+=m_particle_emitter_data.emit_speed;
		while(m_new_frame_number>=1.0){
			speed.y = ParticleTools::rand_glfloat(m_particle_emitter_data.start_speed_max,m_particle_emitter_data.start_speed_min);
			angle = ParticleTools::rand_glfloat(0,MATH_PI*2);
			disperse = ParticleTools::rand_glfloat(0,m_particle_emitter_data.disperse_max);
			speed.x = cos(angle)*disperse;
			speed.z = sin(angle)*disperse;
			ParticleTools::rand_glcolor(m_particle_emitter_data.start_color_min,m_particle_emitter_data.start_color_max,start_color);
			ParticleTools::rand_glcolor(m_particle_emitter_data.end_color_min,m_particle_emitter_data.end_color_max,end_color);
			start_size = ParticleTools::rand_glfloat(m_particle_emitter_data.start_size_min,m_particle_emitter_data.start_size_max);
			end_size = ParticleTools::rand_glfloat(m_particle_emitter_data.end_size_min,m_particle_emitter_data.end_size_max);
			life = ParticleTools::rand_gluint(m_particle_emitter_data.life_min,m_particle_emitter_data.life_max);
			particle_node = new ParticleNode(speed,start_color,end_color,start_size,end_size,life);
			m_list_node.push_back(particle_node);
			--m_new_frame_number;
		}
	}
	for(m_node=m_list_node.begin();m_node != m_list_node.end();){
		m_cur_node = m_node++;
		if((*m_cur_node)->draw(m_particle_emitter_data.acceleration)){
			m_list_node.erase(m_cur_node);
			delete *m_cur_node;
		}
	}
}
}
